package yl.hs.bmipfull.controllers.laboratory;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.laboratory.ProductConfigDetailsEntity;
import yl.hs.bmipfull.service.laboratory.IProductConfigDetailsService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.List;

/**
 * 作用:  产品配比明细表控制层<br/>
 * 创建时间: 2021年05月26日 10时36分49秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "产品配比明细表Web接口", tags = "实验室管理——产品配比明细表操作")
@RequestMapping(value = "/api/product")
@RestController
public class ProductConfigDetailsController extends ControllerBase {

    @Autowired
    IProductConfigDetailsService service;

    @ApiOperation(value = "查询产品配比明细表", notes = "查询产品配比明细表")
    @RequestMapping(value = "/configDetail", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询产品配比明细表", notes = "查询产品配比明细表")
    @RequestMapping(value = "/useConfig/{configSN}/{tasksn}", method = RequestMethod.GET)
    public WebResult queryList(@ApiParam(value = "配比编号") @PathVariable String configSN,@ApiParam(value = "配比编号") @PathVariable String tasksn) {
        try {
            var details = service.findByConfigSn(configSN,tasksn);
            return success("查询成功", details);
        } catch (Exception e) {
            return error(e);
        }
    }


    @ApiOperation(value = "查询产品配比明细表", notes = "根据产品配比明细表编号查询产品配比明细表")
    @RequestMapping(value = "/configDetail/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存产品配比明细表", notes = "查询/更新产品配比明细表信息")
    @RequestMapping(value = "/configDetail", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody List<ProductConfigDetailsEntity> dates) {
        Integer result = 0;
        try {
            if (dates != null && dates.size() > 0 && dates.get(0) != null) {
                String configSN = dates.get(0).getConfigSN();
                result = service.saveProductConfigDetails(dates, configSN);
            }

            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除产品配比明细表信息", notes = "根据产品配比明细表编号删除系产品配比明细表信息")
    @RequestMapping(path = "/configDetail", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "产品配比编号", name = "sn", allowMultiple = true, required = true) @RequestParam("configSn") String configSn) {
        if (service.deleteByConfigSn(configSn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}